import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Window 2.2

/*
倒计时小程序
*/

Item {
    id: root
    width: 240
    height: 240

    property var txtday: ""
    property var newdate: new Date("2021-02-12 00:00:00")
    property var newyear: newdate.getFullYear()
    property var newmonth: newdate.getMonth()
    property var newday: newdate.getDate()
    property var newhour: newdate.getHours()
    property var newminute: newdate.getMinutes()
    property var newsec: newdate.getSeconds()


    function run(){
        var date = new Date

        var msec = parseInt((newdate.getTime() - date.getTime())/1000)

        var lesssec = msec % 60
        var lesssec_other = parseInt(msec / 60)

        var lessminute = lesssec_other % 60
        var lessminute_other = parseInt(lesssec_other / 60)

        var lesshour = lessminute_other % 24
        var lessday = parseInt(lessminute_other / 24)

        txtday = "距离春节还剩" + lessday + "天 " + addZero(lesshour) + ":" + addZero(lessminute) + ":" + addZero(lesssec)

//        console.log( lessday, lesshour, lessminute, lesssec)  // "距离春节还剩" + day + "天"
    }
    function addZero(v){
        if(v<10)
            return '0' + v
        return v
    }

    Component.onCompleted: {
        view.setX(Screen.width - root.width - 5)
        view.setY(Screen.height - root.height - 45)
    }

    Timer{
        id: timer
        triggeredOnStart: true
        interval: 500
        repeat: true
        running: true
        onTriggered: {
            run()
        }
    }

    MouseArea {
        id: dragRegion
        anchors.fill: parent
        property point clickPos: "0,0"
        acceptedButtons: Qt.RightButton | Qt.LeftButton
        onPressed: {
            clickPos  = Qt.point(mouse.x,mouse.y)
            if(mouse.button === Qt.RightButton){
                btnClose.x = mouse.x-btnClose.width/2
                btnClose.y = mouse.y-btnClose.height/2
                btnClose.visible = true
            }
            else{
                btnClose.visible = false
            }
        }
        onPositionChanged: {
            //鼠标偏移量
            var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
            //如果mainwindow继承自QWidget,用setPos
            view.setX(view.x+delta.x)
            view.setY(view.y+delta.y)
        }
    }

    Button{
        id: btnClose
        text: "退出"
        z: 10
        visible: false
        onClicked: view.close()
    }

//    Rectangle{
//        anchors.fill: parent
//        color: Qt.rgba(0,0,0,0)
//        border.width: 1
//        border.color: "red"
//    }

    AnimatedImage{
        source: "qrc:/a.gif"
    }

    Text{
        id: txt
        anchors.horizontalCenter: parent.horizontalCenter
        text: txtday   //"距离春节还剩" + day + "天"
        color: "white"
        y: 30
        font.pixelSize: 18
        font.family: "微软雅黑"

//        SequentialAnimation{
//            running: true
//            loops: -1
//            ColorAnimation{ target: txt; property: "color"; to: "#e54d42"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#f37b1d"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#fbbd08"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#8dc63f"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#39b54a"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#1cbbb4"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#0081ff"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#6739b6"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#9c26b0"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#e03997"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#a5673f"; duration: 5000; }
//            ColorAnimation{ target: txt; property: "color"; to: "#8799a3"; duration: 5000; }
//        }
    }
}
